package com.ruoyi.wechatapi.controller;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.consultation.domain.LabConsultationInfo;
import com.ruoyi.consultation.service.ILabConsultationInfoService;
import com.ruoyi.labdata.domain.LabFlowTask;
import com.ruoyi.labdata.service.ILabFlowTaskService;
import com.ruoyi.labsafety.domain.LabSafetyCheckTask;
import com.ruoyi.labsafety.service.ILabSafetyCheckTaskService;
import com.ruoyi.wechatapi.domain.QueryEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author lzlong
 * @version 1.0
 * @date 2022/6/6 9:10
 * @Description
 */
@RestController
@RequestMapping("/api/home")
public class ConsultationController extends BaseController {

    @Autowired
    private ILabConsultationInfoService labConsultationInfoService;
    @Autowired
    private ILabFlowTaskService labFlowTaskService;
    @Autowired
    private ILabSafetyCheckTaskService labSafetyCheckTaskService;


    @GetMapping("/getInformationList")
    public JSONObject getInformationList(QueryEntity queryEntity) {
        JSONObject jsonObject = new JSONObject();
        //咨讯
        if (queryEntity.getType() == 0){
           jsonObject = getInfoList(queryEntity, jsonObject);
        }
        //我的任务
        if (queryEntity.getType() == 1){
            jsonObject = getMyTaskList(queryEntity, jsonObject);
        }
        //我的代办
        if (queryEntity.getType() == 4){
           jsonObject = getMyAgentList(queryEntity, jsonObject);
        }
        return jsonObject;
    }

    //我的资讯详情查询
    @GetMapping(value = "/getInformationDetail")
    public AjaxResult getInformationDetail(@RequestParam("consultationId") Long consultationId,@RequestParam("type") Long type) {
        if (0==type){
            // 我的资讯
            return AjaxResult.success(labConsultationInfoService.selectLabConsultationInfoById(consultationId));
        }else if (1==type){
            //我的任务
            return AjaxResult.success(labConsultationInfoService.selectLabConsultationInfoById(consultationId));
        }
        return AjaxResult.success();
    }


    private JSONObject getMyTaskList(QueryEntity queryEntity, JSONObject jsonObject) {

        LabSafetyCheckTask labSafetyCheckTask = new LabSafetyCheckTask();
        //Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
        //labSafetyCheckTask.setLoginUserId(userId);
        HashMap<String, Object> map = new HashMap<>();
        PageHelper.startPage(queryEntity.getPageNum(), queryEntity.getPageSize());
        List<LabSafetyCheckTask> list = labSafetyCheckTaskService.selectLabSafetyCheckTaskList(labSafetyCheckTask);
        ArrayList<HashMap<String, Object>> list1 = new ArrayList<>();
        for (LabSafetyCheckTask labSafetyCheckTask1 : list) {
            HashMap<String, Object> map1 = new HashMap<>();
            map1.put("id",labSafetyCheckTask1.getInstanceId());
            map1.put("title",labSafetyCheckTask1.getFlowName());
            map1.put("date", DateUtil.format(labSafetyCheckTask1.getCreateTime(),"yyyy-MM-dd HH:mm:ss"));
            map1.put("name",labSafetyCheckTask1.getCreateBy());
            map1.put("num",0);
            list1.add(map1);
        }
        map.put("count",new PageInfo(list).getTotal());
        map.put("list",list1);
        jsonObject.put("code",200);
        jsonObject.put("message","Succeed");
        jsonObject.put("data",map);
        return jsonObject;
    }


    private JSONObject getInfoList(QueryEntity queryEntity, JSONObject jsonObject) {
        LabConsultationInfo labConsultationInfo = new LabConsultationInfo();
        HashMap<String, Object> map = new HashMap<>();
        PageHelper.startPage(queryEntity.getPageNum(), queryEntity.getPageSize());
        List<LabConsultationInfo> list = labConsultationInfoService.selectLabConsultationInfoList(labConsultationInfo);
        ArrayList<HashMap<String, Object>> list1 = new ArrayList<>();
        for (LabConsultationInfo consultationInfo : list) {
            HashMap<String, Object> map1 = new HashMap<>();
            map1.put("id",consultationInfo.getConsultationId());
            map1.put("title",consultationInfo.getSubjectName());
            map1.put("date", DateUtil.format(consultationInfo.getCreateTime(),"yyyy-MM-dd HH:mm:ss"));
            map1.put("name",consultationInfo.getTitle());
            map1.put("num",0);
            list1.add(map1);
        }
        map.put("count",new PageInfo(list).getTotal());
        map.put("list",list1);
        jsonObject.put("code",200);
        jsonObject.put("message","Succeed");
        jsonObject.put("data",map);
        return jsonObject;
    }

    private JSONObject getMyAgentList(QueryEntity queryEntity, JSONObject jsonObject) {
        HashMap<String, Object> map = new HashMap<>();
        LabFlowTask labFlowTask = new LabFlowTask();
        SysUser loginUser = SecurityUtils.getLoginUser().getUser();
        labFlowTask.setCreateBy(loginUser.getUserName());
        labFlowTask.setCreateById(loginUser.getUserId());
        PageHelper.startPage(queryEntity.getPageNum(), queryEntity.getPageSize());
        List<LabFlowTask> list = labFlowTaskService.selectTodoTaskList(labFlowTask);
        ArrayList<HashMap<String, Object>> list1 = new ArrayList<>();
        for (LabFlowTask labFlowTask1 : list) {
            HashMap<String, Object> map1 = new HashMap<>();
            map1.put("id",labFlowTask1.getTaskId());
            map1.put("title",labFlowTask1.getFlowName());
            map1.put("date", labFlowTask1.getSignTime()==null?"":labFlowTask1.getSignTime().toString());
            map1.put("name",labFlowTask1.getNodeName());
            map1.put("num",0);
            list1.add(map1);
        }
        map.put("count",new PageInfo(list).getTotal());
        map.put("list",list1);
        jsonObject.put("code",200);
        jsonObject.put("message","Succeed");
        jsonObject.put("data",map);
        return jsonObject;
    }
}
